//
//  WelcomeViewController.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/12.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = backImage
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

       setUI()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    func setUI(){
        
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        //欢迎文字的约束
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            
        }

        
        iconView.sd_setImageWithURL(UserAccountViewModel().imageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
    }
    
    //开始动画
    func startAnimation(){
        
        iconView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 180)
            
        }
       
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
             UIView.animateWithDuration(0.6, animations: { () -> Void in
                
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                    
                    NSNotificationCenter.defaultCenter().postNotificationName("tiaozhuan", object: nil)
             })
                
        }
        
    }
    
    //背景图片的懒加载
    lazy var backImage :UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    //头像的懒加载
    lazy var iconView :UIImageView = UIImageView()
    
    //欢迎文字的懒加载
    lazy var welcomeLabel :UILabel = {
    
        var l = UILabel()
        
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(19)
        l.textColor = UIColor.lightGrayColor()
        l.text = UserAccountViewModel().userName! + " 欢迎归来"
        l.sizeToFit()
        l.alpha = 0
        return l
    
    }()
    

}
